import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 26727
 * Date: 2025-10-05
 * Time: 22:33
 */
public class Test101 {

//    public static int findDuplicate(int[] nums) {
//        Map<Integer,Integer> hash = new HashMap<>();
//
//        for(int x : nums) {
//            hash.put(x,hash.getOrDefault(x,0)+1);
//        }
//
//        for(Map.Entry<Integer,Integer> entry : hash.entrySet()) {
//            if(entry.getValue() > 1) {
//                return entry.getKey();
//            }
//        }
//        return -1;
//    }
    public static int findDuplicate(int[] nums) {
        int slow = 0, fast = 0;
        slow = nums[slow];
        fast = nums[nums[fast]];

        while(slow != fast) {
            slow = nums[slow];
            fast = nums[nums[fast]];
        }

        fast = 0;

        while(slow != fast) {
            slow = nums[slow];
            fast = nums[fast];
        }

        return fast;
    }


    public static void main(String[] args) {
        int[] num = {1,3,4,2,2};
        int ret = findDuplicate(num);
        System.out.println(ret);
    }
}
